package com.freeSoft;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.ResultSetMetaData;
import java.util.Properties;


public class CreateBeanFromMeta {

	ResultSetMetaData rs;
	Properties prop;
	String tableName;
	public ResultSetMetaData getRs() {
		return rs;
	}
	public void setRs(ResultSetMetaData rs) {
		this.rs = rs;
	}
	public Properties getProp() {
		return prop;
	}
	public void setProp(Properties prop) {
		this.prop = prop;
	}
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	
	public void generateBean(){
		//check fould exist,if no then create
		String beanPath = prop.getProperty(Util.PROP_BEAN_PATH);
		String outputDir = Util.getOutputDir(prop);
		String projactName = prop.getProperty(Util.PROJECT_NAME);
		String absolutePath = outputDir+ projactName + File.separator+ projactName+ Util.CORE_NAME +File.separator+ "src"+File.separator+"main"+File.separator+"java"+File.separator + Util.javaPackToFilePath(beanPath);
		File f = new File(absolutePath);
		if ( !f.exists()){
			f.mkdirs();
		}
		
		String beanName = tableName.substring(0, 1).toUpperCase()+tableName.substring(1, tableName.length());
		beanName = Util.changeName(beanName);
 
		File file = new File(absolutePath+File.separator+beanName+".java");
		try {
			PrintWriter out	   = new PrintWriter(new BufferedWriter( new FileWriter(file)));
			StringBuffer headerArea = new StringBuffer(); 
			headerArea.append("package "+prop.getProperty(Util.PROP_BEAN_PATH)+";\r\n");
			headerArea.append("\r\n");
			headerArea.append("import java.io.Serializable;\r\n");
			StringBuffer fieldArea = new StringBuffer();
			fieldArea.append("public class "+beanName +" implements Serializable {\r\n");
			fieldArea.append("	/**	serialVersionUID  */\r\n");
			fieldArea.append("	private static final long serialVersionUID = 1L;\r\n");
			for(int i=1;i<=rs.getColumnCount();i++){
				String typeName = rs.getColumnTypeName(i);
				
				String fieldName = rs.getColumnName(i);
				String importPath = Util.getImportPath(typeName);
				if ( ! importPath.equals("")){
					headerArea.append("import "+importPath+"\r\n");
				}
				String fieldType =Util.changeJDBCTypeToJavaType(typeName);
				if ( fieldType.equals("")){
					fieldType = Util.getDataType(rs.getColumnType(i), rs.getScale(i));
				}
				fieldArea.append("	private "+  fieldType + "  " + fieldName +";\r\n");
//				System.out.println("typeName:"+typeName + ";fieldName:"+fieldName+";columenType:"+rs.getColumnType(i));
			}
			
			out.append( headerArea.toString());
			out.append(fieldArea.toString());
			
			for(int i=1;i<=rs.getColumnCount();i++){
				String typeName = rs.getColumnTypeName(i);
				String fieldName = rs.getColumnName(i);
				String fieldType =Util.changeJDBCTypeToJavaType(typeName);
				if ( fieldType.equals(""))
					fieldType = Util.getDataType(rs.getColumnType(i), rs.getScale(i));
				String tmpFieldName = fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1, fieldName.length());
				out.println("	public void set"+tmpFieldName+"("+fieldType+"  "+fieldName +") {");
				out.println("		this."+fieldName+"="+fieldName+";");
				out.println("	}");
				out.println("	public "+ fieldType +" get"+tmpFieldName+"() {");
				out.println("		return this."+fieldName+";");
				out.println("	}");
			}
			out.println("}");
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
